import 'package:decimal/decimal.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ui/api/config.dart';
import 'package:ui/api/db.dart';
import 'package:ui/view/home/logic.dart';

class AddArchivesLogic extends GetxController {
  late Profile? incoming;
  final HomeLogic homeLogic = Get.put(HomeLogic());
  @override
  void onInit() {
    super.onInit();
    incoming = Get.arguments; //传入
    if (incoming != null) {
      mapCtrl.text = incoming!.name;
      selectDate.value = incoming!.time;
      rmbctrl.text = incoming!.rmb;
      isSelectedValue.value = incoming!.is_checkout;
    }
  }

  late RxBool mapIseditOk = RxBool(true);
  late DateTime now = DateTime.now();
  late TextEditingController pricectrl = TextEditingController(text: "");
  late TextEditingController numberctrl = TextEditingController(text: "");
  InputDecoration dec =
      const InputDecoration(border: OutlineInputBorder(), hintText: "0.00");
  // 付款状态
  RxInt isSelectedValue = RxInt(1);
  // 时间
  late RxString selectDate =
      RxString("${now.year}-${now.month.toString()}-${now.day}");

  // 地点
  final TextEditingController mapCtrl = TextEditingController(text: "XX超市");
  //钱
  late TextEditingController rmbctrl = TextEditingController(text: "");

  save() async {
    debugPrint("保存按钮");
    if (rmbctrl.text != "") {
      // 确保金额有效
      if (incoming == null) {
        Profile data = Profile(
          name: mapCtrl.text,
          time: selectDate.value,
          rmb: rmbctrl.text,
          is_checkout: isSelectedValue.value,
        );
        // 是新建档案
        await db.insProfile(data);
      } else {
        Profile data = Profile(
          id: incoming!.id,
          name: mapCtrl.text,
          time: selectDate.value,
          rmb: rmbctrl.text,
          is_checkout: isSelectedValue.value,
        );
        // 是更新档案
        await db.updateProfile(data);
      }
      // 添加地点
      var m = Maptable(name: mapCtrl.text);
      await db.insMap(m);
    }
    // 更新显示
    homeLogic.onReady();
    Get.back();
  }

  onChanged(String str) {
    if (pricectrl.text != "" && numberctrl.text != "") {
      Decimal price = Decimal.parse(pricectrl.text);
      Decimal number = Decimal.parse(numberctrl.text);
      Decimal rmb = price * number;
      rmbctrl.text = rmb.toStringAsFixed(2);
    }
  }

  // 更新选择
  onValueChanged(int index) {
    isSelectedValue.value = index;
  }

  TextStyle listTileFontSize = const TextStyle(fontSize: 16);

  // 选择地点
  locationSelect(context) async {
    var maplist = await db.queryMap();
    // for (var i in maplist) {
    //   var j = Maptable.fromMap(i);
    //   print(j.name);
    // }
    showModalBottomSheet(
        context: context,
        builder: (_) {
          return Container(
            padding: const EdgeInsets.all(5),
            height: 800,
            child: ListView.builder(
                itemCount: maplist.length,
                itemBuilder: (ctx, i) {
                  return TextButton(
                    onPressed: () {
                      mapCtrl.text = maplist[i]['name'].toString();
                      Navigator.of(context).pop();
                    },
                    child: Text(Maptable.fromMap(maplist[i]).name),
                  );
                }),
          );
        });
  }

  // 选择地点 长按
  locationSelectLong() {
    debugPrint("可编辑地点");
    mapIseditOk.value = false;
  }
}
